<?php
// +----------------------------------------------------------------------
// | HkCms 模版商店
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use common\controller\AdminBase;

class Templateshop extends AdminBase
{
	public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 在线模板列表
     * @return mixed
     */
    public function index()
    {
		if ($this->request->isPost()) {
            $this->redirect('index', $_POST);
        }
        $parameter = array(
            'page' => input(config('paginate.var_page'))? : 1,
        );
        $keyword = input('keyword', '', 'trim');
        $parameter['type'] = $id = input('catid', 'template', 'trim');

        if (!empty($keyword)) {
            $parameter['keyword'] = $keyword;
        }
        $this->assign('keyword', $keyword)->assign('catid',$id);
        if ($this->request->isAjax()) {
            $data = $this->Cloud->appCenter($parameter,'cms/appserver/getList');
            if (false === $data) {
                exit($this->Cloud->getError());
            }
            $page = $this->page($data['page']['total'], $data['page']['paging']);
           	$this->assign('data', $data['list']);
            $this->assign('Page', $page->show());  
            return $this->fetch('ajax');
        }
        $this->assign('page', $parameter['page']);
        return $this->fetch();
    }

    /**
     * 在线栏目列表
     * @return mixed
     */
    public function catList()
    {
    	 if ($this->request->isAjax()) {
            $data = $this->Cloud->appCenter(['type'=>'template'],'cms/appserver/category');
            if (false === $data) {
                exit($this->Cloud->getError());
            }
           	$this->assign('data', $data);
           	$this->assign('catid', input('catid'));
            return $this->fetch('category');
        }
    }

    /**
     * 云端模版下载安装
     * @return mixed
     */
    public function install()
    {
        $id = input('id', '', 'trim');
        $mark = input('mark', '', 'trim');
        if (empty($id) || empty($mark)) {
            $this->error('请选择需要安装的模版！');
        }
        $this->assign('stepUrl', url('public_step_1', array('id' => $id,'mark'=>$mark)));
        $this->assign('id', $id);
        return $this->fetch();
    }

    /**
     * 目录权限判断通过后获取下载地址进行下载
     */
    public function public_step_1()
    {
        if (\libs\system\RBAC::authenticate('install') !== true) {
            $this->errors('您没有该项权限！');
        }
        $mark = input('mark', '', 'trim');
        $id = input('id', '', 'trim');
        if (empty($id) || empty($mark)) {
            $this->error('请选择需要安装的模版！');
        }
        $cache = cache('Cloud');
        if (!empty($cache)) {
            $this->error('已经有任务在执行，请稍后！');
        }
        //帐号权限检测
        if ($this->Cloud->competence() == false) {
            $this->errors($this->Cloud->getError());
        }

        $path = TEMPLATE_PATH .  $mark;
        //检查是否有同样的录存在
        if (file_exists($path)) {
            $this->errors("目录：{$path} 已经存在，无法安装在同一目录！");
        }
        //获取下载地址
        $packageUrl = $this->Cloud->appCenter(['id'=>$id],'cms/appserver/getPackage');
        if (empty($packageUrl['url'])) {
            $this->errors($this->Cloud->getError());
        } else {
            cache('Cloud', ['mark'=>$mark,'url'=>$packageUrl['url']], 3600);
        }
        //开始下载
        if ($this->CloudDownload->storageFile($packageUrl['url']) !== true) {
            $this->errors($this->CloudDownload->getError());
        }
        $this->success('文件下载完毕！', url('public_step_2', array('package' => $packageUrl['url'])));
    }

    /**
     * 移动目录到模板
     */
    public function public_step_2()
    {
        if (\libs\system\RBAC::authenticate('install') !== true) {
            $this->errors('您没有该项权限！');
        }
        $data = cache('Cloud');
        if (empty($data)) {
            $this->errors('获取不到需要安装的插件信息缓存！');
        }
        $packageUrl = input('package');
        if (empty($packageUrl)) {
            $this->errors('package参数为空！');
        }
        //临时目录名
        $tmp = $this->CloudDownload->getTempFile($packageUrl);
        //插件安装目录
        $addonsPath = TEMPLATE_PATH .$data['mark'] . '/';
        if ($this->CloudDownload->movedFile($tmp, $addonsPath, $packageUrl) !== true) {
            $this->errors($this->CloudDownload->getError());
        }

        $staticPath = SITE_PATH."statics/".$data['mark'] . '/';
        $this->CloudDownload->movedFile($addonsPath."statics/", $staticPath, $addonsPath."statics/");
        $this->success('移动文件到插件目录中，等待安装！', url('public_step_3', array('identification' => $data['mark'])));
    }

    /**
     * 安装插件
     */
    public function public_step_3()
    {
        if (\libs\system\RBAC::authenticate('install') !== true) {
            $this->errors('您没有该项权限！');
        }
        cache('Cloud', NULL);
        $this->success('插件安装成功！');
    }

    /**
     * 获取应用信息
     */
    public function public_explanation()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('缺少参数！');
        }
        $parameter = array(
            'id' => $id
        );
        $data = $this->Cloud->appCenter($parameter,'cms/appserver/showInfo');
        if (false === $data) {
            $this->error($this->Cloud->getError());
        }
        $this->ajaxReturn(array('status' => true, 'data' => $data));
    }

    /**
     * 错误信息
     * @param string $message
     * @param string $jumpUrl
     * @param bool $ajax
     */
    protected function errors($message = '', $jumpUrl = '', $ajax = false)
    {
        cache('Cloud', NULL);
        $this->error($message, $jumpUrl, $ajax);
    }
}
